package com.yunupmall.controller.ford;

import com.yunupmall.api.fordVO.AddressVo;
import com.yunupmall.api.fordentity.Address;
import com.yunupmall.service.ford.service.AddressService;
import com.yunupmall.uits.Result;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/address")
@AllArgsConstructor
public class AddressController {
    @Autowired
    private  AddressService addressService;
    @Autowired
    private final Result result;
    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody AddressVo addressVo){
        try {
            return result.ok(addressService.save(addressVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @PostMapping("/getList")
    @ResponseBody
    public Result<List<AddressVo>> getList(@RequestBody AddressVo addressVo){
        try {
            return result.ok(addressService.getList(addressVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @GetMapping("/del")
    @ResponseBody
    public Result del(@RequestParam Integer id){
        try {
            return result.ok(addressService.del(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 设为默认地址
     */
    @PostMapping("/updateisDefault")
    @ResponseBody
    private Result<String> updateisDefault(@RequestBody AddressVo addressVo){
        try {
            return result.ok(addressService.updateisDefault(addressVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     *订单页获取收获地址
     */
    @PostMapping("/orderGetList")
    public ModelAndView orderGetList(@RequestBody AddressVo addressVo){
        try {
            ModelAndView modelAndView = new ModelAndView();
            List<AddressVo> list = addressService.getList(addressVo);
            modelAndView.addObject("data",list);
            modelAndView.setViewName("udai_shopcart_pay.html");
            return modelAndView;
        }catch (Exception e) {
            return null;
        }
    }
    /**
     *
     */
    @PostMapping("/byid")
    @ResponseBody
    private Result<List<AddressVo>> byid(@RequestBody AddressVo addressVo){
        try {
            return result.ok(addressService.byid(addressVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
